package com.tanran.es.controller;

import com.tanran.es.model.EsBlog;
import com.tanran.es.model.MysqlBlog;
import com.tanran.es.respository.EsBolgCurdRespository;
import com.tanran.es.respository.EsBolgRespository;
import com.tanran.es.respository.MysqlBlogRespository;
import com.tanran.es.vo.Param;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * Created by Enzo Cotter on 2019/12/26.
 */
@RestController
@Slf4j
public class BlogController {
    private static final String MYSQL = "mysql";
    private static final String ES = "es";
    @Autowired
    private MysqlBlogRespository mysqlBlogRespository;
    @Autowired
    private EsBolgRespository esBolgRespository;
    @Autowired
    private EsBolgCurdRespository esBolgCurdRespository;

    @GetMapping("/blogs")
    public Object blogList(){
        Sort sort =  Sort.by(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = PageRequest.of(0, 10, sort);
        List<MysqlBlog> blogs = mysqlBlogRespository.queryAll(pageRequest);
        return  blogs;
    }

    @PostMapping("/search")
    public Object search(@RequestBody Param param){
        HashMap<String, Object> resultMap = new HashMap<>();
        //统计耗时
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        String type=param.getType();
        if(MYSQL.equals(type)){
            Sort sort =  Sort.by(Sort.Direction.DESC, "createTime");
            PageRequest pageRequest = PageRequest.of(0, 10, sort);
            List<MysqlBlog> mysqlBlogs = mysqlBlogRespository.queryBlog(param.getKeyword(),pageRequest);
            resultMap.put("list",mysqlBlogs);
        }else if (ES.equals(type)){
            //查询条件
            BoolQueryBuilder builder = QueryBuilders.boolQuery();
            builder.should(QueryBuilders.matchPhraseQuery("title",param.getKeyword()));
            builder.should(QueryBuilders.matchPhraseQuery("content",param.getKeyword()));
            //排序条件
            FieldSortBuilder fsb = SortBuilders.fieldSort("create_time").order(SortOrder.DESC);
            //分页
            Pageable pageable = PageRequest.of(0, 10);
            NativeSearchQueryBuilder nativeSearchQuery = new NativeSearchQueryBuilder();
            NativeSearchQuery build = nativeSearchQuery.withQuery(builder).withPageable(pageable).withSort(fsb).build();
            Page<EsBlog> search = esBolgRespository.search(build);
            List<EsBlog> content = search.getContent();
            resultMap.put("list",content);
        }else{
            return "未知";
        }
        stopWatch.stop();
        resultMap.put("duration",stopWatch.getTotalTimeMillis());
        return resultMap;
    }

    @GetMapping("/blog/{id}")
    public Object detail (@PathVariable long id){
        Optional<MysqlBlog> mysqlBlog = mysqlBlogRespository.findById(id);
        return  mysqlBlog.isPresent()?mysqlBlog.get():null;
    }
    @GetMapping("/es/test")
    public Object test (String content){
        List<EsBlog> esBlog1 = esBolgCurdRespository.findByContentOrTitle(content,content);
        List<EsBlog> esBlog2 = esBolgRespository.findByContentOrTitle(content, content);
        return esBlog1.equals(esBlog2);
    }
}
